package com.llb666.java.thread.futuretask.demo;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * FutureTask 线程返回值处理
 * @author mac
 */
@Slf4j
public class FutureTaskDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> task = new FutureTask<>(new MyCallable());
        new Thread(task).start();
        if(!task.isDone()){
            System.out.println("task has not finished, please wait!");
        }
        System.out.println("task return: " + task.get());
    }
    /**
     * 如何实现处理线程的返回值？
     * 通过Callable接口实现：通过FutureTask或者线程池获取
     * @author mac
     */
     static class MyCallable implements Callable<String> {
        @Override
        public String call() throws Exception{
            String value="test";
            System.out.println("Ready to work");
            Thread.sleep(5000);
            System.out.println("task done");
            return  value;
        }
    }
}
